package com.citycloud.ccuap.tc.admin;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.mybatis.repository.config.EnableMybatisRepositories;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.client.RestTemplate;

import com.citycloud.ccuap.framework.uuid.SnowflakeIdWorker;

@Configuration("com.citycloud.ccuap.tc.sync")
@EnableMybatisRepositories(value = { "com.citycloud.ccuap.tc.sync.repository",
		"com.citycloud.ccuap.tc.admin.repository", "com.citycloud.ccuap.tc.yq.repository" }, mapperLocations = {
				"classpath*:/com/citycloud/ccuap/commons/sys/*/*Mapper.xml",
				"classpath*:/com/citycloud/ccuap/tc/sync/*/*Mapper.xml" })
@MapperScan(annotationClass = org.apache.ibatis.annotations.Mapper.class, value = { "com.citycloud.ccuap.tc.sync.*",
		"com.citycloud.ccuap.tc.yq.*", "com.citycloud.ccuap.tc.admin.*" })
public class Configurer implements ResourceLoaderAware {
	@SuppressWarnings("unused")
	private ResourceLoader resourceLoader;

	@Value("${snowflakeId.dataCenterId:0}")
    private long dataCenterId;
	
	@Value("${snowflakeId.workerId:0}")
    private long workerId;
	
	@Autowired
	private RestTemplateBuilder builder;

	@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}

	@Bean("restTemplate")
	public RestTemplate restTemplate() {
		return builder.build();
	}

	@Bean
	public BCryptPasswordEncoder passwordEncoder() {
		// 设置默认的加密方式
		return new BCryptPasswordEncoder();
	}
	
	@Bean
	public SnowflakeIdWorker snowflakeIdWorker() {
		return new SnowflakeIdWorker(dataCenterId, workerId);
	}
}
